package com.df.tv.launcher.view;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.telecom.tv189.comlib.util.Log;

public class MarqueeTextView extends TextView {

    /** 是否停止滚动 */
    private boolean mStopMarquee = false;
    private float mCoordinateX;
    private float textWidth;

    private int recordRepeatTime = -1;
    /** 一个像素滚动时间,刷新时间 */
    private static final int pearPxTime = 10;

    /** 执行中 滚动次数 */
    private int repeatTime = recordRepeatTime;
    /** 完成一次滚动的时间，即滚动速度 */
    private int speed = 50000;
    /** 每次开始滚动的延迟时间 */
    private int pearDelayTime = 2000;

    /** 一次滚动的像素 */
    private float pearPx = 1;
    /** 累计更新 */
    private float pearUpdatePx = 0;

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        stop();
    }

    /**
     * start to move
     */
    private void inital() {
        setVisibility(View.VISIBLE);;
        textWidth = getPaint().measureText(getText().toString());
        double mViewWidth = getMeasuredWidth();
        double dd = ((mViewWidth + textWidth) / speed) * pearPxTime;
        pearPx = ((int) (dd * 100)) / 100.0f;
        if (pearPx == 0) {
            pearPx = 0.01f;
        }
        mCoordinateX = getMeasuredWidth();
        repeatTime = recordRepeatTime;
    }

    public void stop() {
        setVisibility(View.INVISIBLE);
        mStopMarquee = true;
        invalidate();
    }

    /**
     * after setText method
     */
    public void start() {
        if (mHandler.hasMessages(1))
            mHandler.removeMessages(1);
        mHandler.sendEmptyMessageDelayed(1, 500);
    }

    /**
     * @param repeatTime
     *            the repeat Time to set
     */
    public void setRepeatTime(int repeatTime) {
        this.recordRepeatTime = repeatTime;
    }

    /**
     * @param speed
     *            The speed of millisecond to set
     */
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    /**
     * @param pearDelayTime
     *            the pearDelayTime to set
     */
    public void setPearDelayTime(int pearDelayTime) {
        this.pearDelayTime = pearDelayTime;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!mStopMarquee && !TextUtils.isEmpty(getText().toString()))
            canvas.translate(mCoordinateX, 0);
        // canvas.drawText(getText().toString(), mCoordinateX, 15, getPaint());
        super.onDraw(canvas);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                if (Math.abs(getMeasuredWidth() - mCoordinateX) >= (getMeasuredWidth() + textWidth)) {
                    mCoordinateX = getMeasuredWidth();
                    invalidate();
                    Log.d("Marquee", "one end " + repeatTime);

                    if (repeatTime >= 0)
                        repeatTime--;

                    if (repeatTime == 0) {
                        stop();
                    }

                    if (!mStopMarquee) {
                        sendEmptyMessageDelayed(0, pearDelayTime);
                        // sendEmptyMessage(0);
                    }
                }
                else {
                    pearUpdatePx += pearPx;
                    // the less 1 px
                    if (pearUpdatePx >= 1) {
                        mCoordinateX -= pearUpdatePx;// px/time
                        invalidate();
                        pearUpdatePx = 0;
                    }

                    if (!mStopMarquee) {
                        sendEmptyMessageDelayed(0, pearPxTime);
                    }
                }
                break;
            case 1:
                inital();
                mStopMarquee = false;
                sendEmptyMessage(0);
                Log.d("Marquee", "roll Start ");

                break;
            }
            super.handleMessage(msg);
        }
    };

}